﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StorageManagement;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using Utils;
using System.Text.RegularExpressions;
using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Security.Cryptography;
using System.Net.Mail;

namespace __TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><records><record><priority>urgent</priority><error_content>Test Error</error_content><assigned_to>tuandq</assigned_to><system>emarketing</system><severity>3</severity><created_on>2014-04-26 11:19:47</created_on><summary>Test Error</summary></record></records>";
            string b = a.ToMD5Hash();
            string c;
            using (System.Security.Cryptography.MD5 md5Hash = System.Security.Cryptography.MD5.Create())
            {
                c = GetMd5Hash(md5Hash, a);
            }
            Console.ReadLine();
        }

        static string GetMd5Hash(System.Security.Cryptography.MD5 md5Hash, string input)
        {

            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }
        
        public class Test : IDisposable
        {
            public int ID { get; set; }
            public string value1 { get; set; }
            public string value2 { get; set; }
            public string value3 { get; set; }
            public string value4 { get; set; }
            public int ID2 { get; set; }

            public Test() { }

            public Test(int id, string value1, string value2, string value3, string value4, int id2)
            {
                this.ID = id;
                this.value1 = value1;
                this.value2 = value2;
                this.value3 = value3;
                this.value4 = value4;
                this.ID2 = id2;
            }

            #region Disposable
            // A base class that implements IDisposable. 
            // By implementing IDisposable, you are announcing that 
            // instances of this type allocate scarce resources. 

            // Pointer to an external unmanaged resource. 
            private IntPtr Ptrhandle;
            // Track whether Dispose has been called. 
            private bool disposed = false;

            // Implement IDisposable. 
            // Do not make this method virtual. 
            // A derived class should not be able to override this method. 
            public void Dispose()
            {
                Dispose(true);
                // This object will be cleaned up by the Dispose method. 
                // Therefore, you should call GC.SupressFinalize to 
                // take this object off the finalization queue 
                // and prevent finalization code for this object 
                // from executing a second time.
                GC.SuppressFinalize(this);
            }

            // Dispose(bool disposing) executes in two distinct scenarios. 
            // If disposing equals true, the method has been called directly 
            // or indirectly by a user's code. Managed and unmanaged resources 
            // can be disposed. 
            // If disposing equals false, the method has been called by the 
            // runtime from inside the finalizer and you should not reference 
            // other objects. Only unmanaged resources can be disposed. 
            protected virtual void Dispose(bool disposing)
            {
                // Check to see if Dispose has already been called. 
                if (!this.disposed)
                {
                    // If disposing equals true, dispose all managed 
                    // and unmanaged resources. 
                    if (disposing)
                    {
                        // Dispose managed resources.
                    }

                    // Call the appropriate methods to clean up 
                    // unmanaged resources here. 
                    // If disposing is false, 
                    // only the following code is executed.
                    CloseHandle(Ptrhandle);
                    Ptrhandle = IntPtr.Zero;

                    // Note disposing has been done.
                    disposed = true;
                }
            }

            // Use interop to call the method necessary 
            // to clean up the unmanaged resource.
            [System.Runtime.InteropServices.DllImport("Kernel32")]
            private extern static Boolean CloseHandle(IntPtr handle);
            #endregion Dispose

        }
    }
}
